#include "includes.h"

int main (int argc, char *argv[])
{
	board myBoard;
	board originalBoard;
	Move myMove = Move(0,0);
	unsigned int mask = 0;
	undoer unDo;

	newBoard(&myBoard);

	//once for white
	for(unsigned int i = A6; i > 0; i = i >> 1)
	{
		for(unsigned int j = A6; j > 0; j = j >> 1)
		{
			//a black piece on the to spot but white is on move
			if ((i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							  myBoard.p)) && (myBoard.onMove == WHITE))
			{
				continue;
			}
			//a white piece on the to spot but black is on move
			else if ((i & (myBoard.K | myBoard.Q | myBoard.B | myBoard.N | myBoard.R | 
							  myBoard.P)) && (myBoard.onMove == BLACK))
			{
				continue;
			}
			//black would collide with black
			else if ((i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							  myBoard.p)) && collideBlack(&myBoard, j))
			{
				continue;
			}
			//white would collide with white
			else if ((i & (myBoard.K | myBoard.Q | myBoard.B | myBoard.N | 
							       myBoard.R | myBoard.P)) && collideWhite(&myBoard, j))
			{
				continue;
			}
			else if (i == j) //to space is the same as from space
				continue;
			//no piece on from space
			else if (!(i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							        myBoard.p | myBoard.K | myBoard.Q | myBoard.B | myBoard.N | 
											myBoard.R | myBoard.P)))
			{
				continue;
			}
			else
			{
				myMove = Move(i, j);
				memcpy(&originalBoard, &myBoard, sizeof(board));
				unDo = move(&myBoard, myMove);
				unmove(&myBoard, myMove, unDo);
				if(memcmp(&originalBoard, &myBoard, sizeof(board)))
				{
					printf("unmove() failed to return board to pre-move() condition.\n");
					printf("from = ");
					printMask(i);
					printf(" (");
					printSpace(i);
					printf(")\n");
					printf("to = ");
					printMask(j);
					printf(" (");
					printSpace(j);
					printf(")\n\noriginalBoard:");

					dumpBoard(&originalBoard);

					printf(")\n\nmyBoard:");

					dumpBoard(&myBoard);

					exit(EXIT_FAILURE);
				}
			}
		}
	}

	myBoard.onMove = BLACK;

	//once for black
	for(unsigned int i = A6; i > 0; i = i >> 1)
	{
		for(unsigned int j = A6; j > 0; j = j >> 1)
		{
			//a black piece on the to spot but white is on move
			if ((i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							  myBoard.p)) && (myBoard.onMove == WHITE))
			{
				continue;
			}
			//a white piece on the to spot but black is on move
			else if ((i & (myBoard.K | myBoard.Q | myBoard.B | myBoard.N | myBoard.R | 
							  myBoard.P)) && (myBoard.onMove == BLACK))
			{
				continue;
			}
			//black would collide with black
			else if ((i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							  myBoard.p)) && collideBlack(&myBoard, j))
			{
				continue;
			}
			//white would collide with white
			else if ((i & (myBoard.K | myBoard.Q | myBoard.B | myBoard.N | 
							       myBoard.R | myBoard.P)) && collideWhite(&myBoard, j))
			{
				continue;
			}
			else if (i == j) //to space is the same as from space
				continue;
			//no piece on from space
			else if (!(i & (myBoard.k | myBoard.q | myBoard.b | myBoard.n | myBoard.r | 
							        myBoard.p | myBoard.K | myBoard.Q | myBoard.B | myBoard.N | 
											myBoard.R | myBoard.P)))
			{
				continue;
			}
			else
			{
				myMove = Move(i, j);
				memcpy(&originalBoard, &myBoard, sizeof(board));
				unDo = move(&myBoard, myMove);
				unmove(&myBoard, myMove, unDo);
				if(memcmp(&originalBoard, &myBoard, sizeof(board)))
				{
					printf("unmove() failed to return board to pre-move() condition.\n");
					printf("from = ");
					printMask(i);
					printf(" (");
					printSpace(i);
					printf(")\n");
					printf("to = ");
					printMask(j);
					printf(" (");
					printSpace(j);
					printf(")\n\noriginalBoard:");

					dumpBoard(&originalBoard);

					printf(")\n\nmyBoard:");

					dumpBoard(&myBoard);

					exit(EXIT_FAILURE);
				}
			}
		}
	}
}

